<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
//require('echarts/theme/macarons'); // echarts theme
import resize from './mixins/resize'
import { getOneWeekWorkNumberStatics } from "@/api/statistics" 

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '314px'
    }
  },
  data() {
    return {
      chart: null,
      xAxisWeek: [],
      workNumbers: [],
      stationsWorkNumbers: [],
      option: {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
            label: {
              backgroundColor: '#6a7985'
            }
          },
          textStyle: {
            color: '#fff',
            fontStyle: 'normal',
            fontFamily: '微软雅黑',
            fontSize: 12
          }
        },
        grid: {
          top: 35,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          data: [],
          textStyle: {
            color: '#fff'
          },
          selected: {

          }
        },
        xAxis: [
          {
            type: 'category',
            data: [],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#fff'],
                opacity: 0.1
              }
            }
          }
        ],
        series: []
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      let day = new Date().getDay()
      let weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
      this.xAxisWeek = []
      this.xAxisWeek.push(...weeks.slice(day))
      this.xAxisWeek.push(...weeks.slice(0, day))
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(this.option)
      this.asyncGetOneWeekWorkNumberStatics()
    },

    updateChart() {
      // this.workNumbers = [12,33,44,55,66,77,88]
      // this.stationsWorkNumbers = {'站A': [32,43,54,65,76,87,98], '站B': [4,33,6,55,9,66,32], '站C': [5,33,44,7,66,8,9],}

      this.option.series = []
      this.option.legend.data = []

      if (this.workNumbers.length > 0) {
        this.option.legend.data.push('全线')
        this.option.series.push({
            name: '全线',
            type: 'bar',
            barGap: '0',
            barWidth: 10,
            data: this.workNumbers,
            animationDuration,
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0],
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 1,
                      color: 'rgba(127, 128, 225, 0.7)'
                    },
                    {
                      offset: 0.9,
                      color: 'rgba(72, 73, 181, 0.7)'
                    },
                    {
                      offset: 0.31,
                      color: 'rgba(0, 208, 208, 0.7)'
                    },
                    {
                      offset: 0.15,
                      color: 'rgba(0, 208, 208, 0.7)'
                    },
                    {
                      offset: 0,
                      color: 'rgba(104, 253, 255, 0.7)'
                    }
                  ],
                  false
                )
              }
            }
          })
      }

      Object.keys(this.stationsWorkNumbers).forEach(key => {
        let stnName = key
        let stnWorkNumber = this.stationsWorkNumbers[key]
        this.option.legend.data.push(stnName)

        this.option.legend.selected[stnName] = false

        this.option.series.push({
            name: stnName,
            type: 'line',
            barGap: '0',
            barWidth: 10,
            data: stnWorkNumber,
            animationDuration,
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            }
          })
      })

      this.option.xAxis[0].data = this.xAxisWeek
      this.chart.setOption(this.option)
    },

    async asyncGetOneWeekWorkNumberStatics() {
      const res = await getOneWeekWorkNumberStatics()
      if (res.code === 0 && res.data.datesWork.length > 0) {
        this.workNumbers = []
        this.stationsWorkNumbers = {}
        this.xAxisWeek = []

        let datesWorks = res.data.datesWork
        for (let i = 0; i < datesWorks.length; i++) {
          let datesWork = datesWorks[i]
          this.xAxisWeek.push(datesWork.date)
          this.workNumbers.push(datesWork.workNumber)
          Object.keys(datesWork.stationsWorkNumber).forEach(key => {
            if (!(key in this.stationsWorkNumbers)) {
              this.stationsWorkNumbers[key] = []
            }
            this.stationsWorkNumbers[key].push(datesWork.stationsWorkNumber[key])
          })
        }
        this.updateChart()
      }
    }
  }
}
</script>
